var _ffmpegLoaded = false; var _ffmpegInstance = null; function _ctEnsureFfmpeg() { if (_ffmpegInstance) return Promise.resolve(_ffmpegInstance); return loadScriptPromise('https://unpkg.com/\u0040ffmpeg/ffmpeg\u00400.11.6/dist/ffmpeg.min.js').then(function() { var ns = window.FFmpeg || {}; var createFFmpeg = ns.createFFmpeg; var fetchFile = ns.fetchFile; if (!createFFmpeg) throw new Error('FFmpeg library failed to load'); var ffmpeg = createFFmpeg({ log: false, corePath: 'https://unpkg.com/\u0040ffmpeg/core\u00400.11.0/dist/ffmpeg-core.js' }); _ffmpegInstance = { ffmpeg: ffmpeg, fetchFile: fetchFile }; return ffmpeg.load().then(function(){ _ffmpegLoaded = true; return _ffmpegInstance; }); }); } function processFile(blob, fileName) { var isWebm = false; var outExt = isWebm ? 'webm' : 'mp4'; var outMime = isWebm ? 'video/webm' : 'video/mp4'; var notice = document.getElementById('waiting'); _ctEnsureFfmpeg().then(function(ctx) { var ffmpeg = ctx.ffmpeg, fetchFile = ctx.fetchFile; ffmpeg.FS('writeFile', 'in.gif', new Uint8Array(0)); return fetchFile(blob).then(function(buf) { ffmpeg.FS('writeFile', 'in.gif', buf); var args = isWebm ? ['-i', 'in.gif', '-c:v', 'libvpx', '-b:v', '1M', '-auto-alt-ref', '0', 'out.webm'] : ['-i', 'in.gif', '-movflags', 'faststart', '-pix_fmt', 'yuv420p', '-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2', 'out.mp4']; return ffmpeg.run.apply(ffmpeg, args).then(function() { var data = ffmpeg.FS('readFile', 'out.' + outExt); var outBlob = new Blob([data.buffer], { type: outMime }); add_file_output(URL.createObjectURL(outBlob), (fileName || 'animation').replace(/\.[^.]+$/, '.' + outExt)); }); }); }).catch(function(err) { alert('Could not convert this GIF: ' + (err && err.message || err) + '. The first run downloads ~30 MB of conversion code — please check your connection and try again.'); }); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }